#!/bin/bash -xe

kata_coco_top_dir="$HOME/workspace/CoCo/"

kata_containers_repo_url="https://gitee.com/hanliyang-kata-coco/kata-containers.git"
kata_containers_repo_branch="3.13.0-hygon"
kata_containers_repo_dir="${kata_coco_top_dir}/kata-containers"

if [ ! -e ${kata_containers_repo_dir} ]; then
  git clone ${kata_containers_repo_url} \
    -b ${kata_containers_repo_branch} \
    ${kata_containers_repo_dir}
fi

export KERNEL_MODULES_DIR="/dev/shm/openeuler-kernel/lib/modules/6.6.0+/"
export AGENT_TARBALL="/dev/shm/kata-agent/kata-agent.tar.xz"
export COCO_GUEST_COMPONENTS_TARBALL="/dev/shm/guest-components.tar.xz"
export PAUSE_IMAGE_TARBALL="${kata_containers_repo_dir}/tools/packaging/kata-deploy/local-build/build/kata-static-pause-image.tar.xz"

export distro="ubuntu" # example
export rootfs_builder_dir="$(realpath ${kata_containers_repo_dir}/tools/osbuilder/rootfs-builder)"
export image_builder_dir="$(realpath ${kata_containers_repo_dir}/tools/osbuilder/image-builder)"
export ROOTFS_DIR="$(realpath ${kata_containers_repo_dir}/tools/osbuilder/rootfs-builder/rootfs)"


# check
pr_err () {
  echo -e "\033[31;7m[Error] $*\033[0m"
}

check_prerequisites () {
  local elems=(${KERNEL_MODULES_DIR} \
               ${AGENT_TARBALL} \
               ${COCO_GUEST_COMPONENTS_TARBALL} \
               ${PAUSE_IMAGE_TARBALL} \
              )
  for elem in ${elems[@]}; do
    if [ ! -e ${elem} ]; then
       pr_err "missing ${elem}"
       exit 1
    fi
  done
}

check_prerequisites

# build image: (INIT=no, AGENT_INIT=no)

build_rootfs_for_image () {
  echo "Build rootfs for kata guest image"
  pushd ${rootfs_builder_dir}

  sudo rm -rf "${ROOTFS_DIR}"

  # MEASURED_ROOTFS=yes
  script -fec 'sudo -E USE_DOCKER=true AGENT_TARBALL=${AGENT_TARBALL} CONFIDENTIAL_GUEST=yes COCO_GUEST_COMPONENTS_TARBALL=${COCO_GUEST_COMPONENTS_TARBALL} KERNEL_MODULES_DIR=${KERNEL_MODULES_DIR} MEASURED_ROOTFS=yes AGENT_POLICY=yes PAUSE_IMAGE_TARBALL=${PAUSE_IMAGE_TARBALL} ./rootfs.sh "${distro}"'

  # MEASURED_ROOTFS=no
  #script -fec 'sudo -E USE_DOCKER=true AGENT_TARBALL=${AGENT_TARBALL} CONFIDENTIAL_GUEST=yes COCO_GUEST_COMPONENTS_TARBALL=${COCO_GUEST_COMPONENTS_TARBALL} KERNEL_MODULES_DIR=${KERNEL_MODULES_DIR} AGENT_POLICY=yes PAUSE_IMAGE_TARBALL=${PAUSE_IMAGE_TARBALL} ./rootfs.sh "${distro}"'

  popd # ${rootfs_builder_dir}
}

build_rootfs_for_image

build_install_image () {
  echo "Build and Install kata guest image"
  pushd ${image_builder_dir}

  # MEASURED_ROOTFS=yes
  script -fec 'sudo -E USE_DOCKER=true AGENT_TARBALL=${AGENT_TARBALL} CONFIDENTIAL_GUEST=yes COCO_GUEST_COMPONENTS_TARBALL=${COCO_GUEST_COMPONENTS_TARBALL} KERNEL_MODULES_DIR=${KERNEL_MODULES_DIR} MEASURED_ROOTFS=yes AGENT_POLICY=yes PAUSE_IMAGE_TARBALL=${PAUSE_IMAGE_TARBALL} ./image_builder.sh "${ROOTFS_DIR}"'

  # MEASURED_ROOTFS=no
  #script -fec 'sudo -E USE_DOCKER=true AGENT_TARBALL=${AGENT_TARBALL} CONFIDENTIAL_GUEST=yes COCO_GUEST_COMPONENTS_TARBALL=${COCO_GUEST_COMPONENTS_TARBALL} KERNEL_MODULES_DIR=${KERNEL_MODULES_DIR} AGENT_POLICY=yes PAUSE_IMAGE_TARBALL=${PAUSE_IMAGE_TARBALL} ./image_builder.sh "${ROOTFS_DIR}"'

  install_top_dir=/opt/kata
  install_image_dir=$install_top_dir/share/kata-containers/
  mkdir -p $install_image_dir || true
  commit="$(git log --format=%h -1 HEAD)"
  date="$(date +%Y-%m-%d-%T.%N%z)"
  image="kata-containers-${date}-${commit}-csv.img"
  sudo install -o root -g root -m 0644 -D kata-containers.img "$install_image_dir/${image}"
  cd $install_image_dir && sudo ln -sf "${image}" kata-containers-confidential-csv.img

  popd # ${image_builder_dir}
}

build_install_image
